package com.xiaohai.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaohai.common.R;
import com.xiaohai.projo.*;
import com.xiaohai.projo.dto.ScoreDto;
import com.xiaohai.service.CourseService;
import com.xiaohai.service.ScoreService;
import com.xiaohai.service.StudentService;
import com.xiaohai.service.TeacherService;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Iterator;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author xiaohai
 * @since 2024-02-24
 */
@Slf4j
@RestController
@RequestMapping("/score")
public class ScoreController {
    @Autowired
    private ScoreService scoreService;
    @Autowired
    private TeacherService teacherService;
    @Autowired
    private StudentService studentService;
    @Autowired
    private CourseService courseService;
    @Autowired
    private HttpServletRequest request;


    @GetMapping("/list")
    public R<Page<ScoreDto>> list(Integer currentPage, Integer pageSize, String studentName, String courseName, String name, String teacherName, Integer identity, String account) {
        Page<ScoreDto> list = scoreService.myList(currentPage, pageSize, studentName, courseName, teacherName, name, identity,account);
        return R.success(list, "获取数据成功");
    }

    @GetMapping
    public R<ScoreDto> detail(String id) {
        ScoreDto aClass = scoreService.detail(id);
        if (Objects.isNull(aClass)) {
            return R.error("获取详情失败", 201);
        }
        return R.success(aClass, "获取详情成功");
    }

    @DeleteMapping
    public R<Object> deleteById(Integer id) {
        log.info("id:{}", id);
        boolean b = scoreService.delete(id);
        if (b) {
            return R.success(null, "删除成功");
        }
        return R.error("删除失败", 201);
    }

    @GetMapping("/teacherList")
    public R<List<Teacher>> teacherList(String courceId) {
        if (Objects.isNull(courceId) || courceId.isEmpty()) {
            List<Teacher> list = teacherService.list();
            return R.success(list, "获取数据成功");
        }
        Course course = courseService.getById(courceId);
        Integer teaId = course.getTeaId();
        LambdaQueryWrapper<Teacher> teacherLambdaQueryWrapper = new LambdaQueryWrapper<>();
        teacherLambdaQueryWrapper.eq(teaId != null, Teacher::getId, teaId);
        List<Teacher> list = teacherService.list(teacherLambdaQueryWrapper);
        if (Objects.isNull(list)) {
            return R.error("获取数据失败", 201);
        }
        return R.success(list, "获取数据成功");
    }

    @GetMapping("/courseList")
    public R<List<Course>> courseList(Integer identity,String account) {
        List<Course> list = null;
        list = courseService.list();
        if (identity.equals(3)) {
            LambdaQueryWrapper<Teacher> teacherLambdaQueryWrapper = new LambdaQueryWrapper<>();
            teacherLambdaQueryWrapper.eq(account != null, Teacher::getAccount, account);
            Teacher teacher = teacherService.getOne(teacherLambdaQueryWrapper);
            Integer id = teacher.getId();
            LambdaQueryWrapper<Course> courseLambdaQueryWrapper = new LambdaQueryWrapper<>();
            courseLambdaQueryWrapper.eq(id != null, Course::getTeaId, id);
            list = courseService.list(courseLambdaQueryWrapper);
        }

        return R.success(list, "获取数据成功");
    }

    @GetMapping("/studentList")
    public R<List<Student>> studentList(Integer identity) {
        List<Student> list = null;
//        if (identity.equals(3)) {
//
//        }
        list = studentService.list();
        return R.success(list, "获取数据成功");
    }

    @PostMapping
    public R<Objects> addAndUpdate(@RequestBody Score score) {
        log.info("score:{}", score);
        Integer id = score.getId();
        String score1 = score.getScore();
        Integer courceId = score.getCourceId();
        Course course = courseService.getById(courceId);
        Integer max = course.getMax();

        if (id != null) { // 修改
            Boolean b = scoreService.myUpdate(score);
            if (Integer.parseInt(score1) > max) {
                return R.error("修改失败，分数不能大于满分" + max, 201);
            }
            if (b) {
                return R.success(null, "修改成功");
            }
            return R.error("修改失败", 201);

        } else {
            if (Integer.parseInt(score1) > max) {
                return R.error("添加失败，分数不能大于满分" + max, 201);
            }
            boolean add = scoreService.add(score);
            if (add) {
                return R.success(null, "新增成功");
            }
            return R.error("新增失败", 201);
        }
    }

    @PostMapping("/batchAdd")
    public R<Objects> batchAdd(@RequestBody List<Score> scores) {
        Iterator<Score> iterator = scores.stream().iterator();
        while (iterator.hasNext()) {
            Score score = iterator.next();
            Integer id = score.getId();
            String score1 = score.getScore();
            Integer courceId = score.getCourceId();
            Course course = courseService.getById(courceId);
            Integer max = course.getMax();
            if (Integer.parseInt(score1) > max) {
                return R.error("添加失败，分数不能大于满分" + max, 201);
            }
        }
        Boolean b = scoreService.batchAdd(scores);
        if (b) {
            return R.success(null, "新增成功");
        }
        return R.error("新增失败", 201);
    }
}
